import item from '../base/item'
import { UI_Listen } from '../../config/ui_def'
import { Arms } from '../../config/play_def'

cc.Class({
    extends: item,

    properties: {
        light: cc.Node,
        lock: cc.Node,
        red: cc.Node,
    },

    // LIFE-CYCLE CALLBACKS:

    showLight() {
        this.light.active = true
        this.isChoose = true
    },


    hideLight() {
        this.light.active = false
        this.isChoose = false
    },
    //装备init 若为0则装备栏为空
    infoInit(info, index) {
        this.info = info
        this.index = index
        this.isChoose = false
        // let name = `skill_up_${info.id}`
        // this.head_sp.spriteFrame = he.load.get('plist/fightMain', cc.SpriteAtlas).getSpriteFrame(name)
        if (info.equips[index] == 0) {
            this.info_lab.node.active = false
        } else {
            this.info_lab.node.active = true
        }
        this.info_lab.string = `Lv${info.equips[index]}`
        if (info.equips[index] == 0) {
            this.lock.active = true
        } else {
            this.lock.active = false
        }
        let roleName = null
        let wapenName = null
        // if (this.info.id ==)
        switch (info.id) {
            case 1001:
                roleName = 'blue'
                break
            case 1002:
                roleName = 'red'
                break
            case 1003:
                roleName = 'pink'
                break
            case 1004:
                roleName = 'yellow'
                break
            case 1005:
                roleName = 'black'
                break
        }
        switch (index) {
            case Arms.MachineGun:
                wapenName = '_gun'
                break
            case Arms.Pistol:
                wapenName = '_pistol'
                break
            case Arms.Bow:
                wapenName = '_arch'
                break
            case Arms.Sword:
                wapenName = '_sword'
                break
        }
        let name = `${roleName}${wapenName}`
        this.head_sp.spriteFrame = he.load.get('equip', cc.SpriteAtlas,'plist').getSpriteFrame(name)
        this.checkRed()
    },

    checkRed() {
        let needShowRed = false
        let lv = this.info.equips[this.index] + 1
        let costGrp = he.db.getDataByArgs("Equip", { equip: this.index, level: lv })
        let roleNeed = he.db.getDataByArgs("Role", { roleId: this.info.id, level: lv })
        if (costGrp == null) {
            needShowRed = false
        } else {
            let costNeed = `${costGrp.cost}#${roleNeed.cost}`
            needShowRed = gg.data.checkItemStr(costNeed, false)
        }
        this.red.active = needShowRed

    },
    //被点击后
    clickServer() {
        // gg.eventManager.emit('equip', { type: UI_Listen.EQUIP_CHOOSE, info: this.info, index: this.index })
        gg.eventManager.emit('equipItem', { code: UI_Listen.EQUIPITEM_ITEMCLICK, info: this.info, index: this.index })
    },
});
